/*
 @description: 新闻
*/
'use strict';
const Controller = require('egg').Controller;
// const utility = require("utility");   用于加密 密码
class NewsController extends Controller {
    async index() {
        const {
            ctx,
            app
        } = this;
        // const { news_name } = ctx.request.query;
        const {
            pid,
            name,
            page = 1,
            number = 5
        } = ctx.request.query;
        // 如果page=2,则offset = 2,翻页为第二页,这个page是根据后台传进来的
        const offset = (page - 1) * number;
        let data, count; //data数据 ， count全部数据的数量
        if (name && page) {
            // 查询数据库管理员表的字段,根据名字模糊查询,
            data = await app.mysql.query(`select * from news where news_name like '%${name}%' limit ${offset},${number}`);
            // like '%${name}%' limit ${offset},${number}  模糊查询(name传进来的名字) ，然后默认查询出来第一页 显示多少条数据 
            // 查询所有的表的数据条数count 
            count = await app.mysql.query(`select count(*) as count from news where news_name like '%${name}%'`);
        } else if (name) {
            data = await app.mysql.query(`select * from news where news_name ='${name}' or news_name  like '%${name}%' limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from news where news_name like '%${name}%'`);
        } else if (pid) {
            data = await app.mysql.query(`select * from   where id like '${pid}' limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from product where id like '${pid}'`);
        } else {
            data = await app.mysql.query(`select * from news limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from news`);
        }

        if (count[0].count != 0) {
            ctx.body = {
                code: 0,
                data: data,
                count: count[0].count,
                msg: '查询成功'
            }
        } else {
            ctx.body = {
                code: 4001,
                msg: "查询失败,没有数据"
            }
        }
    }
    // async index() {
    //     const {
    //         ctx,
    //         app
    //     } = this;
    //     const {
    //         name,
    //         page = 1,
    //         number = 5
    //     } = ctx.request.query
    //     // 如果page=2,则offset = 2,翻页为第二页,这个page是根据后台传进来的
    //     const offset = (page - 1) * number
    //     let data, count; //data数据 ， count全部数据的数量
    //     if (name && page) {
    //         data = await app.mysql.query(`select id,news_name,news_image from news where name like '%${name}%' limit ${offset},${number}`);
    //         count = await app.mysql.query(`select count(*) as count from news where name like '%${name}%'`);
    //     } else if (name) {
    //         data = await app.mysql.query(`select id,news_name,news_image from news where name ='${name}' or name  like '%${name}%' limit ${offset},${number}`);
    //         count = await app.mysql.query(`select count(*) as count from news where name like '%${name}%'`);
    //     } else {
    //         data = await app.mysql.query(`select id,news_name,news_image from news limit ${offset},${number}`);
    //         count = await app.mysql.query(`select count(*) as count from news`);
    //     }
    //     if (count[0].count != 0) {
    //         ctx.body = {
    //             code: 0,
    //             msg: '查询成功',
    //             data
    //         }
    //     } else {
    //         ctx.body = {
    //             code: 4004,
    //             msg: '查询失败，没有数据',
    //             data
    //         }
    //     }
    // }
    //增加
    async create() {
        const {
            ctx,
            app
        } = this;
        const {
            pid,
            news_name,
            news_image
        } = ctx.request.body;
        const result = await app.mysql.query(`select id from product`);
        // const yanzi = /[\u4e00-\u9fa5]/;
        if (pid && news_name && news_image) {
            // 往数据库添加数据
            const result = await app.mysql.insert('news', {
                news_name,
                news_image,
                pid
            })
            ctx.body = {
                code: 0,
                msg: '添加成功',
                data: result
            }

        } else {
            ctx.body = {
                code: 4004,
                msg: '添加失败',
            }
        }
    }
    //删
    async destroy() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        // 删除与 数据库id 相同的一串数据  +id转成 number类型
        const result = await app.mysql.delete('news', {
            id: +id
        });
        if (result) {
            ctx.body = {
                code: 0,
                msg: '删除成功',
                data: result
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: '删除失败，请正确输入',
            }
        }

    }
    //改
    async update() {
        const {
            ctx,
            app
        } = this;
        const {
            news_name,
            news_image
        } = ctx.request.body;
        const {
            id
        } = ctx.params;
        const row = {
            news_name: news_name,
            news_image: news_image
        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('news', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: '修改完成',
                data: result
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: '修改失败，请正确输入',
            }
        }
    }
    //查询
    async show() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.get('news', {
            id: +id
        });
        if (result) {
            ctx.body = {
                code: 0,
                msg: '查询成功',
                data: result
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: '请正确输入想要查询的id',
            }
        }
    }

}

module.exports = NewsController;